package com.itheima.huikebackend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.huikebackend.context.BaseContext;
import com.itheima.huikebackend.dto.DicCreateDTO;
import com.itheima.huikebackend.dto.DictListDTO;
import com.itheima.huikebackend.entity.SysDictType;
import com.itheima.huikebackend.mapper.DictionaryMapper;
import com.itheima.huikebackend.service.DictionaryService;
import com.itheima.huikebackend.vo.SysDictDataVO;
import com.itheima.huikebackend.vo.SysDictListVO;
import com.itheima.huikebackend.vo.SysDictTypeExcelVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class DictionaryServiceImpl implements DictionaryService {

    private final DictionaryMapper dictionaryMapper;

    @Override
    public Map list(DictListDTO dictListDTO) {
        PageHelper.startPage(dictListDTO.getPageNum(),dictListDTO.getPageSize());
        Page<SysDictType> page = dictionaryMapper.selectPage(dictListDTO);

        Map<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    @Override
    public List<SysDictDataVO> queryType(String type) {
        List<SysDictDataVO> list = dictionaryMapper.selectType(type);
        return list;
    }

    @Override
    public SysDictListVO queryById(Long dictId) {
        SysDictListVO sysDictListVO = dictionaryMapper.selectDictTypeById(dictId);
        return sysDictListVO;
    }

    @Override
    public Map queryTypeList(DictListDTO dictListDTO) {
        PageHelper.startPage(dictListDTO.getPageNum(),dictListDTO.getPageSize());
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dictListDTO,sysDictType);
        Page<SysDictDataVO> page = dictionaryMapper.selectTypeList(sysDictType);
        Map<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    @Override
    public void create(DicCreateDTO dicCreateDTO) {
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dicCreateDTO,sysDictType);
        //记录登录id
        sysDictType.setCreateBy( BaseContext.getCurrentId());
        sysDictType.setCreateTime(LocalDateTime.now());

        dictionaryMapper.insert(sysDictType);

    }

    @Override
    public void delete(List<Long> ids) {
        dictionaryMapper.deleteByDicId(ids);
    }

    @Override
    public void update(DicCreateDTO dicCreateDTO) {
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(dicCreateDTO,sysDictType);
        sysDictType.setUpdateBy(BaseContext.getCurrentId());
        sysDictType.setUpdateTime(LocalDateTime.now());

        dictionaryMapper.updateByDictId(sysDictType);
    }

    @Override
    public void export(HttpServletResponse response) {
        try {
            //1.查询到excel表
            InputStream inp = this.getClass().getClassLoader().getResourceAsStream("index.xlsx");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheetAt0 = wb.getSheet("Sheet1");
            List<SysDictTypeExcelVO> list = dictionaryMapper.selectAll();



            for (int i = 0; i < list.size(); i++) {
                SysDictTypeExcelVO sysDictTypeExcelVO = list.get(i);
                Row row = sheetAt0.createRow(i + 1);

                Cell cell1 = row.createCell(0);
                Cell cell2 = row.createCell(1);
                Cell cell3 = row.createCell(2);
                Cell cell4 = row.createCell(3);
                Cell cell5 = row.createCell(4);
                Cell cell6 = row.createCell(5);

                cell1.setCellValue(sysDictTypeExcelVO.getDictId());
                cell2.setCellValue(sysDictTypeExcelVO.getDictName());
                cell3.setCellValue(sysDictTypeExcelVO.getDictType());
                cell4.setCellValue(sysDictTypeExcelVO.getStatus());
                cell5.setCellValue(sysDictTypeExcelVO.getRemark());
                cell6.setCellValue(sysDictTypeExcelVO.getCreateTime().toString());

            }

            //2.写到response响应中
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);

        } catch (Exception e) {
            log.info("出错了：{}",e.getMessage());
        }
    }

}
